package com.itheima.advantagesup.controller;

import com.github.pagehelper.PageHelper;
import com.itheima.advantagesup.common.R;
import com.itheima.advantagesup.entity.SysMenu;
import com.itheima.advantagesup.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;

@Slf4j
@RequestMapping("/sys_menu")
@RestController
public class MenuController {
    @Autowired
    private MenuService menuService;

    /**
     * 添加
     *
     * @param sysMenu 菜单签证官
     * @return {@link R}
     */
    @PostMapping("/add")
    public R add(@RequestBody SysMenu sysMenu) {
        log.info("添加菜单:{}", sysMenu);
        menuService.add(sysMenu);
        return R.success();
    }

    /**
     * 更新菜单
     *
     * @param sysMenu 系统菜单
     * @return {@link R}<{@link ?}>
     */
    @PostMapping("/update")
    public R<?> updateMenu(@RequestBody SysMenu sysMenu) {
        menuService.updateMenu(sysMenu);
        return R.success();
    }

    /**
     * 查询系统菜单通过id
     *
     * @param id id
     * @return {@link R}<{@link SysMenu}>
     */
    @GetMapping("/rowInfo")
    private R<SysMenu> querySysMenuById(Integer id) {
        SysMenu sysMenu = menuService.querySysMenuById(id);
        return R.success(sysMenu);
    }


    /**
     * 查询所有数据
     *
     * @return {@link R}<{@link List}<{@link SysMenu}>>
     */
    @GetMapping("/allData")
    public R<List<SysMenu>> queryAllData(){
        List<SysMenu> list = menuService.queryAllData();
        return R.success(list);
    }

    /**
     * 删除菜单
     *
     * @param id id
     * @return {@link R}
     */
    @GetMapping("/del")
    public R delMenu(Integer id){
        menuService.delMenu(id);
        return R.success();
    }
}
